package com.empire.module.member.controller.admin.accounttype;

import com.empire.module.member.dal.dataobject.accounttype.AccountTypeDO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.io.IOException;
import java.util.List;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.accounttype.vo.*;
import com.empire.module.member.service.accounttype.AccountTypeService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 账号平台")
@RestController
@RequestMapping("/member/account-type")
@Validated
public class AccountTypeController {

    @Resource
    private AccountTypeService accountTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建账号平台")
    @PreAuthorize("@ss.hasPermission('member:account-type:create')")
    public CommonResult<Long> createAccountType(@Valid @RequestBody AccountTypeSaveReqVO createReqVO) {
        return success(accountTypeService.createAccountType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账号平台")
    @PreAuthorize("@ss.hasPermission('member:account-type:update')")
    public CommonResult<Boolean> updateAccountType(@Valid @RequestBody AccountTypeSaveReqVO updateReqVO) {
        accountTypeService.updateAccountType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账号平台")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:account-type:delete')")
    public CommonResult<Boolean> deleteAccountType(@RequestParam("id") Long id) {
        accountTypeService.deleteAccountType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账号平台")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:account-type:query')")
    public CommonResult<AccountTypeRespVO> getAccountType(@RequestParam("id") Long id) {
        AccountTypeDO accountType = accountTypeService.getAccountType(id);
        return success(BeanUtils.toBean(accountType, AccountTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账号平台分页")
    @PreAuthorize("@ss.hasPermission('member:account-type:query')")
    public CommonResult<PageResult<AccountTypeRespVO>> getAccountTypePage(@Valid AccountTypePageReqVO pageReqVO) {
        PageResult<AccountTypeDO> pageResult = accountTypeService.getAccountTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AccountTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得账号平台列表")
//    @PreAuthorize("@ss.hasPermission('member:account-type:query')")
    public CommonResult<List<AccountTypeRespVO>> getAccountTypeList() {
        List<AccountTypeDO> pageResult = accountTypeService.getAccountTypeList();
        return success(BeanUtils.toBean(pageResult, AccountTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出账号平台 Excel")
    @PreAuthorize("@ss.hasPermission('member:account-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAccountTypeExcel(@Valid AccountTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AccountTypeDO> list = accountTypeService.getAccountTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "账号平台.xls", "数据", AccountTypeRespVO.class,
                        BeanUtils.toBean(list, AccountTypeRespVO.class));
    }

}